package com.rz.htk.serviceprovider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.rz.core.exception.GlobalException;
import com.rz.htk.dubbo.service.DictDataService;
import com.rz.htk.print.service.IPrintTemplateService;
import com.rz.htk.serviceprovider.entity.ServiceProvider;
import com.rz.htk.serviceprovider.mapper.ServiceProviderMapper;
import com.rz.htk.serviceprovider.service.IServiceProviderService;
import consumer.model.SysDictData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ServiceProviderServiceImpl
 * @Description 服务商
 * @Author maweiyue
 * @Date 2019/8/16 11:37
 */
@Service
public class ServiceProviderServiceImpl extends ServiceImpl<ServiceProviderMapper, ServiceProvider> implements IServiceProviderService {

    @Resource private DictDataService dictDataService;

    @Resource private IPrintTemplateService printTemplateService;

    @Override
    public IPage<ServiceProvider> getPage(ServiceProvider product) {
        QueryWrapper<ServiceProvider> wrapper = new QueryWrapper<>();
        if (product.getType() != null && !product.getType().equals(0)) {
            wrapper.eq("type", product.getType());
        }
        if (StringUtils.isNotEmpty(product.getName())) {
            wrapper.like("name", product.getName());
        }
        if (StringUtils.isNotEmpty(product.getManager())) {
            wrapper.like("manager", product.getManager());
        }
        if (StringUtils.isNotEmpty(product.getAddress())) {
            wrapper.like("address", product.getAddress());
        }
        wrapper.eq("del_flag", 0);

        Page<ServiceProvider> page = new Page<>(product.getPageNo(), product.getPageSize());
        IPage<ServiceProvider> desktopIPage = page(page, wrapper);
        buildResult(desktopIPage.getRecords());
        return desktopIPage;
    }

    private void buildResult(List<ServiceProvider> records) {

    }

    @Override
    @Transactional
    public boolean saveServiceProvider(ServiceProvider sp){
        boolean isOk = false;
        if (sp.getId() == null) {
            if (this.getByName(sp.getName()) != null) {
                throw new GlobalException("服务商已存在！");
            }
            sp.setDelFlag(0);
            sp.setCrteTime(LocalDateTime.now());
        } else {
            ServiceProvider old = this.getByName(sp.getName());
            if (old != null && !old.getId().equals(sp.getId())) {
                throw new GlobalException("服务商已存在！");
            }
        }
        isOk = this.saveOrUpdate(sp);
        if (!isOk) {
            throw new GlobalException("产品信息保存失败！");
        }
        return isOk;
    }

    public boolean deleteById(Integer id){
        ServiceProvider serviceProvider = this.getById(id);
        if (serviceProvider != null) {
            serviceProvider.setDelFlag(1);
            boolean isOK = this.saveOrUpdate(serviceProvider);
            return isOK;
        } else {
            throw new GlobalException("服务商不存在！");
        }
    }

    public List<ServiceProvider> listByType(Integer type){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("type", type);
        queryWrapper.eq("del_flag", 0);
        return list(queryWrapper);
    }

    private ServiceProvider getByName(String name){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("name", name);
        queryWrapper.eq("del_flag", 0);
        List<ServiceProvider> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }
}
